<template>
  <div class="role-management">
    <div class="page-header">
      <h1>角色管理</h1>
      <el-button type="primary" @click="openRoleForm()">新增角色</el-button>
    </div>

    <el-card shadow="hover">
      <el-table :data="roles" row-key="id">
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="code" label="角色编码" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status ? 'success' : 'danger'">{{ row.status ? '启用' : '禁用' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button link type="primary" @click="openRoleForm(row)">编辑</el-button>
            <el-button link type="danger" @click="deleteRole(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="roleFormVisible" :title="isEdit ? '编辑角色' : '新增角色'" width="600px">
      <el-form :model="roleForm" label-width="100px">
        <el-form-item label="角色名称"><el-input v-model="roleForm.name" /></el-form-item>
        <el-form-item label="角色编码"><el-input v-model="roleForm.code" /></el-form-item>
        <el-form-item label="描述"><el-input type="textarea" v-model="roleForm.description" /></el-form-item>
        <el-form-item label="状态"><el-switch v-model="roleForm.status" /></el-form-item>
        <el-form-item label="权限">
          <el-tree
            ref="treeRef"
            :data="permissionTree"
            show-checkbox
            node-key="id"
            :default-checked-keys="roleForm.permissions"
            :props="{ label: 'name' }"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="roleFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitRoleForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const roleFormVisible = ref(false);
const isEdit = ref(false);
const treeRef = ref(null);

const roles = ref([
  { id: 1, name: '超级管理员', code: 'SUPER_ADMIN', description: '拥有所有权限', status: true, permissions: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] },
  { id: 2, name: '项目管理员', code: 'PROJECT_ADMIN', description: '管理项目相关功能', status: true, permissions: [2, 6, 7, 8] },
  { id: 3, name: '内容审核员', code: 'CONTENT_AUDITOR', description: '审核内容和项目', status: false, permissions: [2, 8] },
]);

const roleForm = reactive({ id: null, name: '', code: '', description: '', status: true, permissions: [] });

const permissionTree = [
  { id: 1, name: '系统管理', children: [ { id: 4, name: '用户管理' }, { id: 5, name: '角色管理' } ] },
  { id: 2, name: '项目管理', children: [ { id: 6, name: '项目列表' }, { id: 7, name: '项目审核' } ] },
  { id: 3, name: '内容管理', children: [ { id: 8, name: '分类管理' } ] },
];

const openRoleForm = (role) => {
  if (role) {
    isEdit.value = true;
    Object.assign(roleForm, role);
  } else {
    isEdit.value = false;
    Object.assign(roleForm, { id: null, name: '', code: '', description: '', status: true, permissions: [] });
  }
  roleFormVisible.value = true;
};

const submitRoleForm = () => {
  roleForm.permissions = treeRef.value.getCheckedKeys();
  if (isEdit.value) {
    const index = roles.value.findIndex(r => r.id === roleForm.id);
    if (index !== -1) roles.value[index] = { ...roleForm };
    ElMessage.success('更新成功');
  } else {
    roles.value.push({ ...roleForm, id: Date.now() });
    ElMessage.success('新增成功');
  }
  roleFormVisible.value = false;
};

const deleteRole = async (role) => {
  await ElMessageBox.confirm(`确定删除角色 "${role.name}"?`, '提示', { type: 'warning' });
  const index = roles.value.findIndex(r => r.id === role.id);
  if (index !== -1) roles.value.splice(index, 1);
  ElMessage.success('删除成功');
};
</script>

<style scoped>
.role-management { max-width: 1200px; margin: 0 auto; }
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
.page-header h1 { font-size: 24px; font-weight: 700; }
</style> 